package com.scud.order.config;

import com.scud.common.constants.RabbitmqConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author vermouth
 */
@Configuration
public class RabbitConfig {

    //声明订单超时队列
    @Bean
    public Queue orderOverQueue(){
        return QueueBuilder.durable(RabbitmqConstants.SCUD_ORDER_OVER_QUEUE)
                .ttl(RabbitmqConstants.DELAY_TIME)
                .deadLetterExchange(RabbitmqConstants.SCUD_ORDER_DEAD_EXCHANGE)
                .deadLetterRoutingKey(RabbitmqConstants.SCUD_ORDER_DEAD_KEY).build();
    }

    //声明订单超时交换机
    @Bean
    public DirectExchange orderOverExchange(){
        return new DirectExchange(RabbitmqConstants.SCUD_ORDER_OVER_EXCHANGE,true,false);
    }

    //绑定订单超时队列和订单超时交换机
    @Bean
    public Binding bindingOverDirect(Queue orderOverQueue,DirectExchange orderOverExchange){
        return BindingBuilder.bind(orderOverQueue)
                .to(orderOverExchange)
                .with(RabbitmqConstants.SCUD_ORDER_OVER_KEY);
    }

    //声明死信队列
    @Bean
    public Queue orderDeadQueue(){
        return QueueBuilder.durable(RabbitmqConstants.SCUD_ORDER_DEAD_QUEUE).build();
    }

    //声明死信交换机
    @Bean
    public DirectExchange orderDeadExchange(){
        return new DirectExchange(RabbitmqConstants.SCUD_ORDER_DEAD_EXCHANGE,true,false);
    }

    //绑定死信队列和交换机
    @Bean
    public Binding bindingDeadDirect(Queue orderDeadQueue,DirectExchange orderDeadExchange){
        return BindingBuilder.bind(orderDeadQueue)
                .to(orderDeadExchange)
                .with(RabbitmqConstants.SCUD_ORDER_DEAD_KEY);
    }
}
